﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;

namespace _4591BRD
{
    /// <summary>
    /// 内存中预处理数据，以天为单位
    /// </summary>
    class CurrentDateManager
    {
        private static CurrentDateManager _instance = null;
        public static CurrentDateManager Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new CurrentDateManager();
                return _instance;
            }
        }

        //当前日期
        private DateTime currentDate;

        //角色数据缓存
        private Dictionary<int, CharDataObj> _charDataCacheDaily; //charid, data
        public Dictionary<int, CharDataObj> CharDataCacheDaily
        {
            get { return _charDataCacheDaily; }
        }
        private Dictionary<int, CharDataObj> _charDataCacheMonthly; //charid, data
        public Dictionary<int, CharDataObj> CharDataCacheMonthly
        {
            get { return _charDataCacheMonthly; }
        }
        private Dictionary<int, CharDataObj> _charDataCacheAll; //charid, data
        public Dictionary<int, CharDataObj> CharDataCacheAll
        {
            get { return _charDataCacheAll; }
        }

        public void ClearCharDataDaily()
        {
            _charDataCacheDaily = new Dictionary<int, CharDataObj>();
        }
        public void ClearCharDataMonthly()
        {
            _charDataCacheMonthly = new Dictionary<int, CharDataObj>();
        }
        public void ClearCharDataAll()
        {
            _charDataCacheAll = new Dictionary<int, CharDataObj>();
        }

        //军团数据缓存
        private Dictionary<int, GameObject> _teamDataCacheDaily; //teamid, data
        public Dictionary<int, GameObject> TeamDataCacheDaily
        {
            get { return _teamDataCacheDaily; }
        }
        private Dictionary<int, GameObject> _teamDataCacheMonthly; //teamid, data
        public Dictionary<int, GameObject> TeamDataCacheMonthly
        {
            get { return _teamDataCacheMonthly; }
        }
        private Dictionary<int, GameObject> _teamDataCacheAll; //teamid, data
        public Dictionary<int, GameObject> TeamDataCacheAll
        {
            get { return _teamDataCacheAll; }
        }

        public void ClearTeamDataDaily()
        {
            _teamDataCacheDaily = new Dictionary<int, GameObject>();
        }
        public void ClearTeamDataMonthly()
        {
            _teamDataCacheMonthly = new Dictionary<int, GameObject>();
        }
        public void ClearTeamDataAll()
        {
            _teamDataCacheAll = new Dictionary<int, GameObject>();
        }

        public CurrentDateManager()
        {
            CheckPath();

            currentDate = DateTime.Now;
            _charDataCacheDaily = new Dictionary<int, CharDataObj>();
            _charDataCacheMonthly = new Dictionary<int, CharDataObj>();
            _charDataCacheAll = new Dictionary<int, CharDataObj>();

            _teamDataCacheDaily = new Dictionary<int, GameObject>();
            _teamDataCacheMonthly = new Dictionary<int, GameObject>();
            _teamDataCacheAll = new Dictionary<int, GameObject>();
        }

        private string logpath;
        private string tmppath;

        private void CheckPath()
        {
            tmppath = Environment.CurrentDirectory + "\\TEMP\\CharData\\";
            if (!Directory.Exists(tmppath))
                Directory.CreateDirectory(tmppath);
            logpath = Environment.CurrentDirectory + "\\Logs\\";
            if (!Directory.Exists(logpath))
                Directory.CreateDirectory(logpath);
        }

        //设置当前日期，如于原日期不同，则初始化
        public void SetCurrentDate(string dateStr)
        {
            DateTime tmpDate = DateTime.Parse(dateStr);

            if (currentDate.CompareTo(tmpDate) != 0)
            {
                currentDate = tmpDate;
            }
        }

        public GameObject GetTeamData(int teamid, int type)
        {           
            //先从数据库中取出数据
            string sql = "";
            string tblName = "";
            SqlDataReader reader;
            string dateStr = currentDate.Year.ToString() + "_" + currentDate.Month.ToString() + "_" + currentDate.Day.ToString();
            string monStr = currentDate.Year.ToString() + "_" + currentDate.Month.ToString();

            string strType = "";
            switch (type)
            {
                case 0:
                    strType = "Daily_" + dateStr;
                    break;
                case 1:
                    strType = "Monthly_" + monStr;
                    break;
            }

            lock (_teamDataCacheDaily)
            {
                if (!_teamDataCacheDaily.ContainsKey(teamid))
                {
                    GameObject objData = new GameObject();
                    /*
                    try
                    {
                        tblName = "tbTeam" + strType;
                        sql = " SELECT * FROM " + tblName + " WITH(NOLOCK) WHERE TeamID=" + teamid.ToString();
                        reader = SqlHelper.ExecuteReader(Common.ConnStrBRD, CommandType.Text, sql, null);
                        if (reader.Read())
                        {
                            objData.AddTeamValue("PointTotal", int.Parse(reader["PointTotal"].ToString()));
                            objData.AddTeamValue("ActiveMember", int.Parse(reader["ActiveMember"].ToString()));
                        }
                        reader.Close();
                    }
                    catch (SqlException se)
                    {
                        Log.WriteError(se.Message);
                    }
                    catch (Exception e)
                    {
                        Log.WriteError(e.Message);
                    }
                    */
                    _teamDataCacheDaily.Add(teamid, objData);
                }
                return _teamDataCacheDaily[teamid];
            }
        }
        
        public CharDataObj GetCharData(int charid, int type)
        {
            //先从数据库中取出数据
            string sql = "";
            string tblName = "";
            SqlDataReader reader;
            string dateStr = currentDate.Year.ToString() + "_" + currentDate.Month.ToString() + "_" + currentDate.Day.ToString();
            string monStr = currentDate.Year.ToString() + "_" + currentDate.Month.ToString();

            string strType = "";
            switch (type)
            {
                case 0:
                    strType = "Daily_" + dateStr;
                    break;
                case 1:
                    strType = "Monthly_" + monStr;
                    break;
            }

            lock (_charDataCacheDaily)
            {
                if (!_charDataCacheDaily.ContainsKey(charid))
                {
                    CharDataObj objData = new CharDataObj();
                    /*
                    try
                    {
                        //日功勋
                        tblName = "tbCharPoint" + strType;
                        sql = " SELECT * FROM " + tblName + " WITH(NOLOCK) WHERE CharID=" + charid.ToString();
                        reader = SqlHelper.ExecuteReader(Common.ConnStrBRD, CommandType.Text, sql, null);
                        if (reader.Read())
                        {
                            objData.AddDataValue("PointTotal", int.Parse(reader["PointTotal"].ToString()));
                            objData.AddDataValue("PointKill", int.Parse(reader["PointKill"].ToString()));
                            objData.AddDataValue("PointHack", int.Parse(reader["PointHack"].ToString()));
                            objData.AddDataValue("PointShare", int.Parse(reader["PointShare"].ToString()));
                            objData.AddDataValue("PointTask", int.Parse(reader["PointTask"].ToString()));
                        }
                        reader.Close();

                        //日击杀
                        tblName = "tbCharKill" + strType;
                        sql = " SELECT * FROM " + tblName + " WITH(NOLOCK) WHERE CharID=" + charid.ToString();
                        reader = SqlHelper.ExecuteReader(Common.ConnStrBRD, CommandType.Text, sql, null);
                        if (reader.Read())
                        {
                            objData.AddDataValue("KillTotal", int.Parse(reader["KillTotal"].ToString()));
                            objData.AddDataValue("KillRobot", int.Parse(reader["KillRobot"].ToString()));
                            objData.AddDataValue("KillExRobot", int.Parse(reader["KillExRobot"].ToString()));
                        }
                        reader.Close();

                        //总死亡
                        tblName = "tbCharDeath";
                        sql = " SELECT * FROM " + tblName + " WITH(NOLOCK) WHERE CharID=" + charid.ToString();
                        reader = SqlHelper.ExecuteReader(Common.ConnStrBRD, CommandType.Text, sql, null);
                        if (reader.Read())
                        {
                            objData.AddDataValue("DeathTotal", int.Parse(reader["DeathTotal"].ToString()));
                            objData.AddDataValue("DeathSuicide", int.Parse(reader["DeathSuicide"].ToString()));
                            objData.AddDataValue("DeathKill", int.Parse(reader["DeathKill"].ToString()));
                            objData.AddDataValue("DeathCrash", int.Parse(reader["DeathCrash"].ToString()));
                            objData.AddDataValue("DeathDrop", int.Parse(reader["DeathDrop"].ToString()));
                            objData.AddDataValue("DeathDrown", int.Parse(reader["DeathDrown"].ToString()));
                            objData.AddDataValue("DeathOther", int.Parse(reader["DeathOther"].ToString()));
                        }
                        reader.Close();

                        //总进入战场
                        tblName = "tbCharEnterBG";
                        sql = " SELECT * FROM " + tblName + " WITH(NOLOCK) WHERE CharID=" + charid.ToString();
                        reader = SqlHelper.ExecuteReader(Common.ConnStrBRD, CommandType.Text, sql, null);
                        if (reader.Read())
                        {
                            objData.AddDataValue("EnterCount", int.Parse(reader["EnterCount"].ToString()));
                            objData.AddDataValue("StayTime", int.Parse(reader["StayTime"].ToString()));
                            objData.AddDataValue("SpawnCount", int.Parse(reader["SpawnCount"].ToString()));
                        }
                        reader.Close();

                        //总击杀目标次数
                        tblName = "tbCharKillObj";
                        sql = " SELECT * FROM " + tblName + " WITH(NOLOCK) WHERE CharID=" + charid.ToString();
                        reader = SqlHelper.ExecuteReader(Common.ConnStrBRD, CommandType.Text, sql, null);
                        if (reader.Read())
                        {
                            objData.AddKillObjValue(int.Parse(reader["TargetID"].ToString()), int.Parse(reader["TotalCount"].ToString()));
                        }
                        reader.Close();

                        //使用武器
                        tblName = "tbCharWeaponUse" + strType;
                        sql = " SELECT * FROM " + tblName + " WITH(NOLOCK) WHERE CharID=" + charid.ToString();
                        reader = SqlHelper.ExecuteReader(Common.ConnStrBRD, CommandType.Text, sql, null);
                        if (reader.Read())
                        {
                            objData.AddWeaponUseValue(int.Parse(reader["WeaponID"].ToString()), int.Parse(reader["FireCount"].ToString()), int.Parse(reader["HitCount"].ToString()));
                        }
                        reader.Close();

                        //使用道具
                        tblName = "tbCharEquipUse" + strType;
                        sql = " SELECT * FROM " + tblName + " WITH(NOLOCK) WHERE CharID=" + charid.ToString();
                        reader = SqlHelper.ExecuteReader(Common.ConnStrBRD, CommandType.Text, sql, null);
                        if (reader.Read())
                        {
                            objData.AddEquipUseValue(int.Parse(reader["EquipID"].ToString()), int.Parse(reader["UseTime"].ToString()));
                        }
                        reader.Close();

                        _charDataCacheDaily.Add(charid, objData);
                    }
                    catch (SqlException se)
                    {
                        Log.WriteError(se.Message);
                    }
                    catch (Exception e)
                    {
                        Log.WriteError(e.Message);
                    }
                     */
                    _charDataCacheDaily.Add(charid, objData);
                }
                return _charDataCacheDaily[charid];
            }
        }

        private void ExecBCP()
        {
            if (Directory.Exists(tmppath))
            {  
                try
                {
                    string commandText = "";
                    string[] files = Directory.GetFiles(tmppath, "*.txt");
                    Log.WriteLine(1, "Start BCP! files = " + files.Length.ToString());     
                    foreach (string file in files)
                    {
                        int start = file.LastIndexOf("\\") + 1;
                        string tblName = file.Substring(start, file.LastIndexOf(".") - start);
                        commandText += "\r\n BCP TY4591_BRD.dbo." + tblName;
                        commandText += " in " + file + " -c ";
                        commandText += System.Configuration.ConfigurationSettings.AppSettings["BCPConnString"].ToString();
                        commandText += " -e " + logpath + "BCP_in_Error_" + tblName + ".log";
                        commandText += "\r\n del " + file;
                    }
                    Common.ExeCommand(commandText);
                    Log.WriteLine(1, "Finish BCP! files = " + files.Length.ToString());   
                }
                catch (IOException e)
                {
                    Log.WriteError("Run BCP Error!!! " + e.Message);
                }
                catch (Exception e)
                {
                    Log.WriteError("Run BCP Error!!! " + e.Message);
                }
                //Log.WriteError(commandText + "\r\n");                
            }
        }

        public void SaveCharData(int itype)
        {
            string monStr = currentDate.Year.ToString() + "_" + currentDate.Month.ToString();
            string DBdateStr = currentDate.ToString("yyyy-MM-dd", System.Globalization.DateTimeFormatInfo.InvariantInfo);
            string dateStr = monStr + "_" + currentDate.Day.ToString();

            string strType = "";
            switch (itype)
            {
                case 0:
                    strType = "Daily_" + dateStr;
                    break;
                case 1:
                    strType = "Monthly_" + monStr;
                    break;
            }

            string dataPointFile = tmppath + "tbCharPoint" + strType + ".txt";
            string dataKillFile = tmppath + "tbCharKill" + strType + ".txt";
            string dataDeathFile = tmppath + "tbCharDeath" + strType + ".txt";
            string dataEnterBGFile = tmppath + "tbCharEnterBG" + strType + ".txt";
            string dataKillObjFile = tmppath + "tbCharKillObj" + strType + ".txt";
            string dataWeaponUseFile = tmppath + "tbCharWeaponUse" + strType + ".txt";
            string dataEquipUseFile = tmppath + "tbCharEquipUse" + strType + ".txt";

            Log.WriteLine(1, "Start Save Char Data! " + strType);

            try
            {
                StreamWriter swPointData = new StreamWriter(dataPointFile, false, Encoding.GetEncoding("GB2312"));
                StreamWriter swKillData = new StreamWriter(dataKillFile, false, Encoding.GetEncoding("GB2312"));
                StreamWriter swDeathData = new StreamWriter(dataDeathFile, false, Encoding.GetEncoding("GB2312"));
                StreamWriter swEnterBGData = new StreamWriter(dataEnterBGFile, false, Encoding.GetEncoding("GB2312"));
                StreamWriter swKillObjData = new StreamWriter(dataKillObjFile, false, Encoding.GetEncoding("GB2312"));
                StreamWriter swWeaponUseData = new StreamWriter(dataWeaponUseFile, false, Encoding.GetEncoding("GB2312"));
                StreamWriter swEquipUseData = new StreamWriter(dataEquipUseFile, false, Encoding.GetEncoding("GB2312"));
                
                List<int> charList = new List<int>();
                int iCount = 0;
                foreach (KeyValuePair<int, CharDataObj> obj in _charDataCacheAll)
                {
                    CharDataObj theCharData = obj.Value;
                    int charid = obj.Key;

                    charList.Add(charid);

                    Dictionary<string, int> objData = theCharData.ObjData;
                    
                    //写入总功勋
                    swPointData.Write(charid + "\t" + objData["PointTotal"].ToString() + "\t" + objData["PointKill"].ToString() + "\t" + objData["PointHack"].ToString() + "\t" + objData["PointShare"].ToString() + "\t" + objData["PointTask"].ToString() + "\t0\t0\t0\t" + DBdateStr + "\r\n");
                    //写入总击杀
                    swKillData.Write(charid + "\t" + objData["KillTotal"].ToString() + "\t" + objData["KillRobot"].ToString() + "\t" + objData["KillExRobot"].ToString() + "\t0\t0\t0\t0\t0\t0\t" + DBdateStr + "\r\n");
                    //写入总死亡
                    swDeathData.Write(charid + "\t" + objData["DeathTotal"].ToString() + "\t" + objData["DeathSuicide"].ToString() + "\t" + objData["DeathKill"].ToString() + "\t" + objData["DeathCrash"].ToString() + "\t" + objData["DeathDrop"].ToString() + "\t" + objData["DeathDrown"].ToString() + "\t" + objData["DeathOther"].ToString() + "\t0\t0\t0\t" + DBdateStr + "\r\n");
                    //写入总进入战场
                    swEnterBGData.Write(charid + "\t" + '0' + "\t" + objData["EnterCount"].ToString() + "\t" + objData["StayTime"].ToString() + "\t" + objData["SpawnCount"].ToString() + "\t0\t0\t0\t" + DBdateStr + "\r\n");

                    //写入击杀目标次数
                    foreach (KeyValuePair<int, int> killobj in theCharData.KillObj)
                    {
                        swKillObjData.Write(charid + "\t" + '0' + "\t0\t" + killobj.Key.ToString() + "\t" + killobj.Value.ToString() + "\t0\t0\t0\t" + DBdateStr + "\r\n");
                    }

                    //写入使用武器
                    swWeaponUseData.Write(charid + "\t" + objData["WeaponID"].ToString() + "\t" + objData["FireCount"].ToString() + "\t" + objData["HitCount"].ToString() + "\t0\t0\t0\t" + DBdateStr + "\r\n");
                    //写入使用道具
                    swEquipUseData.Write(charid + "\t" + objData["EquipID"].ToString() + "\t" + objData["UseTime"].ToString() + "\t0\t0\t0\t" + DBdateStr + "\r\n");

                    iCount++;

                    if (iCount % 50 == 0)
                        Log.WriteLine(1, "Save Char Data = " + iCount.ToString());
                }
                swPointData.Close();
                swKillData.Close();
                swDeathData.Close();
                swEnterBGData.Close();
                swKillObjData.Close();
                swWeaponUseData.Close();
                swEquipUseData.Close();

                //删除数据库里现有数据
                string sqlDelCharPoint = "";
                string sqlDelCharKill = "";
                string sqlDelCharKillObj = "";
                string sqlDelCharDeath = "";
                string sqlDelCharEnterBG = "";
                string sqlDelCharWeaponUse = "";
                string sqlDelCharEquipUse = "";

                string tblCharPoint = "tbCharPoint" + strType;
                string tblCharKill = "tbCharKill" + strType;
                string tblCharKillObj = "tbCharKillObj" + strType;
                string tblCharDeath = "tbCharDeath" + strType;
                string tblCharEnterBG = "tbCharEnterBG" + strType;
                string tblCharWeaponUse = "tbCharWeaponUse" + strType;
                string tblCharEquipUse = "tbCharEquipUse" + strType;

                for (int i = 0; i < charList.Count; i++)
                {
                    string startStr = "";

                    if (i % 1000 == 0)
                    {
                        //结束上一阶段循环，执行SQL
                        if (i > 0)
                        {
                            sqlDelCharPoint += ")";
                            sqlDelCharKill += ")";
                            sqlDelCharKillObj += ")";
                            sqlDelCharDeath += ")";
                            sqlDelCharEnterBG += ")";
                            sqlDelCharWeaponUse += ")";
                            sqlDelCharEquipUse += ")";

                            SqlHelper.ExecuteNonQuery(Common.ConnStrBRD, CommandType.Text, sqlDelCharPoint, null);
                            SqlHelper.ExecuteNonQuery(Common.ConnStrBRD, CommandType.Text, sqlDelCharKill, null);
                            SqlHelper.ExecuteNonQuery(Common.ConnStrBRD, CommandType.Text, sqlDelCharKillObj, null);
                            SqlHelper.ExecuteNonQuery(Common.ConnStrBRD, CommandType.Text, sqlDelCharDeath, null);
                            SqlHelper.ExecuteNonQuery(Common.ConnStrBRD, CommandType.Text, sqlDelCharEnterBG, null);
                            SqlHelper.ExecuteNonQuery(Common.ConnStrBRD, CommandType.Text, sqlDelCharWeaponUse, null);
                            SqlHelper.ExecuteNonQuery(Common.ConnStrBRD, CommandType.Text, sqlDelCharEquipUse, null);
                        }

                        sqlDelCharPoint = "IF EXISTS (SELECT 1 FROM Sysobjects WHERE Type = 'U' AND Name = '" + tblCharPoint + "') \n";
                        sqlDelCharPoint += "DELETE " + tblCharPoint + " WHERE CharID IN (";

                        sqlDelCharKill = "IF EXISTS (SELECT 1 FROM Sysobjects WHERE Type = 'U' AND Name = '" + tblCharKill + "') \n";
                        sqlDelCharKill += "DELETE " + tblCharKill + " WHERE CharID IN (";

                        sqlDelCharKillObj = "IF EXISTS (SELECT 1 FROM Sysobjects WHERE Type = 'U' AND Name = '" + tblCharKillObj + "') \n";
                        sqlDelCharKillObj += "DELETE " + tblCharKillObj + " WHERE CharID IN (";

                        sqlDelCharDeath = "IF EXISTS (SELECT 1 FROM Sysobjects WHERE Type = 'U' AND Name = '" + tblCharDeath + "') \n";
                        sqlDelCharDeath += "DELETE " + tblCharDeath + " WHERE CharID IN (";

                        sqlDelCharEnterBG = "IF EXISTS (SELECT 1 FROM Sysobjects WHERE Type = 'U' AND Name = '" + tblCharEnterBG + "') \n";
                        sqlDelCharEnterBG += "DELETE " + tblCharEnterBG + " WHERE CharID IN (";

                        sqlDelCharWeaponUse = "IF EXISTS (SELECT 1 FROM Sysobjects WHERE Type = 'U' AND Name = '" + tblCharWeaponUse + "') \n";
                        sqlDelCharWeaponUse += "DELETE " + tblCharWeaponUse + " WHERE CharID IN (";

                        sqlDelCharEquipUse = "IF EXISTS (SELECT 1 FROM Sysobjects WHERE Type = 'U' AND Name = '" + tblCharEquipUse + "') \n";
                        sqlDelCharEquipUse += "DELETE " + tblCharEquipUse + " WHERE CharID IN (";
                    }

                    if (i % 1000 != 0)
                        startStr = ",";

                    sqlDelCharPoint += startStr + charList[i].ToString();
                    sqlDelCharKill += startStr + charList[i].ToString();
                    sqlDelCharKillObj += startStr + charList[i].ToString();
                    sqlDelCharDeath += startStr + charList[i].ToString();
                    sqlDelCharEnterBG += startStr + charList[i].ToString();
                    sqlDelCharWeaponUse += startStr + charList[i].ToString();
                    sqlDelCharEquipUse += startStr + charList[i].ToString();
                }

                if (sqlDelCharKillObj != "")
                {
                    sqlDelCharPoint += ")";
                    sqlDelCharKill += ")";
                    sqlDelCharKillObj += ")";
                    sqlDelCharDeath += ")";
                    sqlDelCharEnterBG += ")";
                    sqlDelCharWeaponUse += ")";
                    sqlDelCharEquipUse += ")";

                    SqlHelper.ExecuteNonQuery(Common.ConnStrBRD, CommandType.Text, sqlDelCharPoint, null);
                    SqlHelper.ExecuteNonQuery(Common.ConnStrBRD, CommandType.Text, sqlDelCharKill, null);
                    SqlHelper.ExecuteNonQuery(Common.ConnStrBRD, CommandType.Text, sqlDelCharKillObj, null);
                    SqlHelper.ExecuteNonQuery(Common.ConnStrBRD, CommandType.Text, sqlDelCharDeath, null);
                    SqlHelper.ExecuteNonQuery(Common.ConnStrBRD, CommandType.Text, sqlDelCharEnterBG, null);
                    SqlHelper.ExecuteNonQuery(Common.ConnStrBRD, CommandType.Text, sqlDelCharWeaponUse, null);
                    SqlHelper.ExecuteNonQuery(Common.ConnStrBRD, CommandType.Text, sqlDelCharEquipUse, null);
                }

                //导入数据
                if (iCount > 0)
                {
                    ExecBCP();
                }
            }
            catch (SqlException se)
            {
                Log.WriteError(se.Message);
            }
            catch (Exception e)
            {
                Log.WriteError(e.Message);
            }
            Log.WriteLine(1, "Save Char Data Success! " + strType);
        }

        //功勋
        public void AddPoint(int charid, string locX, string locY, string locZ, string time, string eventData)
        {
            CharDataObj objDataDaily = GetCharData(charid, 0);
            CharDataObj objDataMonthly = GetCharData(charid, 1);
            CharDataObj objDataAll = GetCharData(charid, 2);

            int pType = 0;
            int point = 0;

            string[] evDatas = eventData.Split(' ');
            if (evDatas.Length >= 2)
            {
                pType = int.Parse(evDatas[0]);
                point = int.Parse(evDatas[1]);
            }

            //总击杀
            objDataDaily.AddDataValue("PointTotal", point);
            objDataMonthly.AddDataValue("PointTotal", point);
            objDataAll.AddDataValue("PointTotal", point);

            //击杀
            if (pType == 1)
            {
                objDataDaily.AddDataValue("PointKill", point);
                objDataMonthly.AddDataValue("PointKill", point);
                objDataAll.AddDataValue("PointKill", point);
            }
            else if (pType == 3)
            {
                //破解
                objDataDaily.AddDataValue("PointHack", point);
                objDataMonthly.AddDataValue("PointHack", point);
                objDataAll.AddDataValue("PointHack", point);
            }
            else if (pType == 2 || pType == 6)
            {
                //分享
                objDataDaily.AddDataValue("PointShare", point);
                objDataMonthly.AddDataValue("PointShare", point);
                objDataAll.AddDataValue("PointShare", point);
            }
            else if (pType == 4 || pType == 7)
            {
                //任务
                objDataDaily.AddDataValue("PointTask", point);
                objDataMonthly.AddDataValue("PointTask", point);
                objDataAll.AddDataValue("PointTask", point);
                /*
                //如角色有军团，加军团战绩
                int teamid = NameCacheManager.Instance.GetTeamIDByCharID(charid);
                if (teamid > 0)
                {
                    GameObject teamObjDaily = GetTeamData(teamid, 0);
                    GameObject teamObjMonthly = GetTeamData(teamid, 1);
                    GameObject teamObjAll = GetTeamData(teamid, 2);

                    teamObjDaily.AddTeamValue("PointTotal", 1);
                    teamObjMonthly.AddTeamValue("PointTotal", 1);
                    teamObjAll.AddTeamValue("PointTotal", 1);

                    teamObjDaily.AddActMember(charid, 1);
                    teamObjMonthly.AddActMember(charid, 1);
                    teamObjAll.AddActMember(charid, 1);
                }*/
            }
        }

        //击杀
        public void AddKill(int charid, string locX, string locY, string locZ, string time, string eventData)
        {
            CharDataObj objDataDaily = GetCharData(charid, 0);
            CharDataObj objDataMonthly = GetCharData(charid, 1);
            CharDataObj objDataAll = GetCharData(charid, 2);

            int targetCharID = 0;
            int weapon = 0;
            int pType = 1;

            string[] evDatas = eventData.Split(' ');
            if (evDatas.Length >= 2)
            {
                targetCharID = int.Parse(evDatas[0]);
                weapon = int.Parse(evDatas[1]);
                pType = int.Parse(evDatas[2]); //区分被击杀机器人类型: 1-普通，2-重装
            }

            //使用武器击杀次数
            objDataAll.AddWeaponKillValue(weapon, 1);

            //击杀次数
            objDataDaily.AddDataValue("KillTotal", 1);
            objDataMonthly.AddDataValue("KillTotal", 1);
            objDataAll.AddDataValue("KillTotal", 1);

            if (pType == 1) //击杀机器人
            {
                objDataDaily.AddDataValue("KillRobot", 1);
                objDataMonthly.AddDataValue("KillRobot", 1);
                objDataAll.AddDataValue("KillRobot", 1);
            }
            else if (pType == 2) //击杀重装
            {
                objDataDaily.AddDataValue("KillExRobot", 1);
                objDataMonthly.AddDataValue("KillExRobot", 1);
                objDataAll.AddDataValue("KillExRobot", 1);
            }

            //击杀目标角色次数
            objDataDaily.AddKillObjValue(targetCharID, 1);
            objDataMonthly.AddKillObjValue(targetCharID, 1);
            objDataAll.AddKillObjValue(targetCharID, 1);

            //使用武器击杀次数
            objDataAll.AddWeaponKillValue(weapon, 1);
        }

        //击毁载具
        public void AddKillVehicle(int charid, string locX, string locY, string locZ, string time, string eventData)
        {
            CharDataObj objDataDaily = GetCharData(charid, 0);
            CharDataObj objDataMonthly = GetCharData(charid, 1);
            CharDataObj objDataAll = GetCharData(charid, 2);

            int estType = 0;
            int weapon = 0;

            string[] evDatas = eventData.Split(' ');
            if (evDatas.Length >= 2)
            {
                estType = int.Parse(evDatas[0]) - 200000; //设施ID
                weapon = int.Parse(evDatas[1]);
            }

            //使用武器击杀次数
            objDataAll.AddWeaponKillValue(weapon, 1);

            //击杀载具
            objDataDaily.AddDataValue("KillVehicle", 1);
            objDataMonthly.AddDataValue("KillVehicle", 1);
            objDataAll.AddDataValue("KillVehicle", 1);

            if (NameCacheManager.Instance.PlaneList.ContainsKey(estType)) //飞机
            {
                objDataDaily.AddDataValue("KillPlane", 1);
                objDataMonthly.AddDataValue("KillPlane", 1);
                objDataAll.AddDataValue("KillPlane", 1);
            }
            else
            {
                objDataDaily.AddDataValue("KillTank", 1);
                objDataMonthly.AddDataValue("KillTank", 1);
                objDataAll.AddDataValue("KillTank", 1);
            }

            //使用武器击杀次数
            //objData.AddWeaponKillValue(weapon, 1);
        }

        //死亡
        public void AddDeath(int charid, string locX, string locY, string locZ, string time, string eventData)
        {
            CharDataObj objDataAll = GetCharData(charid, 2);

            int pType = 1;
            int pData = 0;

            string[] evDatas = eventData.Split(' ');
            if (evDatas.Length >0)
            {
                pType = int.Parse(evDatas[0]);                
                if (evDatas.Length > 1)
                    pData = int.Parse(evDatas[1]);
            }

            //总死亡
            objDataAll.AddDataValue("DeathTotal", 1);

            //自毁
            if (pType == 0)
            {
                objDataAll.AddDataValue("DeathSuicide", 1);
            }
            else if (pType == 1)
            {
                //被击毁
                objDataAll.AddDataValue("DeathKill", 1);
            }
            else if (pType == 2)
            {
                //被载具碾死
                objDataAll.AddDataValue("DeathCrash", 1);
            }
            else if (pType == 3)
            {
                //摔死
                objDataAll.AddDataValue("DeathCrash", 1);
            }
            else if (pType == 4)
            {
                //淹死
                objDataAll.AddDataValue("DeathDrown", 1);
            }
            else if (pType == 5)
            {
                //其他
                objDataAll.AddDataValue("DeathOther", 1);
            }
            else if (pType == 6)
            {
                //在载具中死亡
                objDataAll.AddDataValue("DeathKill", 1);
            }
        }

        //使用武器
        public void UseWeapon(int charid, string locX, string locY, string locZ, string time, string eventData)
        {
            CharDataObj objDataAll = GetCharData(charid, 2);

            int weaponID = 0;
            int fireCount = 0;
            int hitCount = 0;

            string[] evDatas = eventData.Split(' ');
            if (evDatas.Length >= 3)
            {
                weaponID = int.Parse(evDatas[0]);
                fireCount = int.Parse(evDatas[1]);
                hitCount = int.Parse(evDatas[2]);
            }

            objDataAll.AddWeaponUseValue(weaponID, fireCount, hitCount);
        }

        //使用道具
        public void UseEquip(int charid, string locX, string locY, string locZ, string time, string eventData)
        {
            CharDataObj objDataAll = GetCharData(charid, 2);

            int equipID = 0;
            int useTime = 0;

            string[] evDatas = eventData.Split(' ');
            if (evDatas.Length >= 2)
            {
                equipID = int.Parse(evDatas[0]);
                useTime = int.Parse(evDatas[1]);
            }

            objDataAll.AddEquipUseValue(equipID, useTime);
        }

        //团队指挥
        public void AddTeamGroup(int charid, string locX, string locY, string locZ, string time, int eventType)
        {

        }

        //进入进出
        public void AddEnterBG(int charid, string locX, string locY, string locZ, string time, string eventData)
        {
            CharDataObj objDataAll = GetCharData(charid, 2);

            objDataAll.AddDataValue("EnterCount", 1);
        }

        //重生
        public void Spawn(int charid, string locX, string locY, string locZ, string time, string eventData)
        {
            CharDataObj objDataAll = GetCharData(charid, 2);

            objDataAll.AddDataValue("SpawnCount", 1);
        }
    }
}
